package flc.ast.activity;


import android.app.Dialog;
import android.media.MediaPlayer;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;
import com.stark.ve.core.SpeedMode;

import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityVideoSpeedBinding;
import flc.ast.view.RulerView;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;

public class VideoSpeedActivity extends BaseAc<ActivityVideoSpeedBinding> {

    public static String videoPath="", speedPath="";
    private float mCurSpeed = 1.0f;
    private float maxValue = 4.0f;
    private float minValue = 0f;
    private Dialog myTipDialog;

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_video_speed;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoSpeedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvGenerate.setOnClickListener(this);
        mDataBinding.ivVideoPlay.setOnClickListener(this);
        mDataBinding.ivVideoSpeedSave.setOnClickListener(this);
        mDataBinding.rulerView.setValue(1f, minValue, maxValue, 0.1f);
        mDataBinding.rulerView.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                mCurSpeed=value;
            }
        });
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivVideoPlay:
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.bofang00);
                    mDataBinding.videoView.pause();
                } else {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting00);
                    mDataBinding.videoView.start();
                }
                break;
            case R.id.tvGenerate:
                videoSpeed();
                break;
            case R.id.ivVideoSpeedSave:
                if (speedPath.isEmpty()){
                    ToastUtils.showShort(getString(R.string.please_generate));
                    return;
                }
                saveVideo();
                break;
            //提示弹框
            case R.id.tvDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.tvDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        //获取视频路径 时长
        if (TextUtils.isEmpty(videoPath)) return;
        //设置播放器
        setPlayer();
    }

    private void videoSpeed() {
        EditorHolder.editor().videoChangeSpeed(videoPath, mCurSpeed, SpeedMode.ALL, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.generate_suc));
                speedPath = outFilePath;
                mDataBinding.videoView.setVideoPath(speedPath);
                mDataBinding.videoView.seekTo(1);
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting00);
                mDataBinding.videoView.start();
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.speed_def));
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.speed_ing) + progress + getString(R.string.unit_percent));
            }
        });
    }

    //保存视频
    private void saveVideo() {
        showDialog(getString(R.string.save_video_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //保存到相册
                        FileP2pUtil.copyPrivateVideoToPublic(mContext, speedPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startActivity(HomeActivity.class);
                            }
                        },500);
                    }
                });
            }
        }, 1000);
    }

    //设置播放器
    private void setPlayer() {
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.bofang00);
                mp.seekTo(1);
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting00);
        mDataBinding.videoView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivVideoPlay.setImageResource(R.drawable.bofang00);
        mDataBinding.videoView.pause();
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.tvDialogTipCancel);
        TextView right = inflate.findViewById(R.id.tvDialogTipRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}